<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="进货单">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();

            ajaxSelect("${baseUrl}organization/store/getStores.action","storeId","全部");
            ajaxSelect("${baseUrl}settAccount/settAccount/listVo.action","settAccountId","全部");
            ajaxSelect("${baseUrl}user/user/getAll.action","userId","全部");

            <c:if test="${date1 != null and date2!= null}">
                $("#selectSearch").click();
            </c:if>
        });

        layui.use(['form', 'table','laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                util = layui.util;

            laydate.render({
                elem: '#date',
                range:'~',
                <c:if test="${date1 != null and date2!= null}">
                value: "${date1} ~ ${date2}",
                </c:if>
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="date1"]').val(getDateFromLayUiDate(date));
                        $('[name="date2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="date1"]').val('');
                        $('[name="date2"]').val('');
                    }
                }
            });

            table.on('toolbar(table)', function (obj) {
                switch(obj.event){
                    case 'export':
                        exportExcel();
                        break;
                };
            });

            form.on('checkbox(noClose)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
                return false;
            });

            renderTable();

        });



        function renderTable() {
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}funds/moneyWater/getList.action",
                totalRow:true,
                where : {
                    date1:"${date1}",
                    date2:"${date2}",
                    close :false,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {width:70,title:"序号",templet:function (d) {
                            return genTableIndex(d.close,"作废",d);
                        }},
                    {width: 100, title: '业务日期',style:"font-size:12px;",templet: function (d) {
                            return GMTToDate(d.date);
                        }},
                    {field: 'no', width: 130, title: '单据编号',style:"font-size:12px;",templet:'#noBar'},
                    {field: 'unitName',width: 150, title: '往来单位名称',style:"font-size:12px;"},
                    {field: 'typeName',width: 140, title: '收支项目名称',style:"font-size:12px;"},
                    {field: 'user',width: 120, title: '经手人',style:"font-size:12px;"},
                    {field: 'store',width: 140, title: '门店',style:"font-size:12px;"},
                    {field: 'settAccount',width: 120, title: '结算账户',style:"font-size:12px;"},

                    {field:'inAmt',width: 90, title: '收入',style:"font-size:12px;",templet: function (d) {
                            return emptyEntity(d.inAmt) ? '' : getPrice(d.inAmt);
                        }},
                    {field:'outAmt',width: 90, title: '支出',style:"font-size:12px;",templet: function (d) {
                            return emptyEntity(d.outAmt) ? '' : getPrice(d.outAmt);
                        }},
                    {field: 'remark',minWidth: 120, title: '备注',style:"font-size:12px;"},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    $(".linkNo").unbind().bind("click", function () {
                        openTab($(this).attr("title"), $(this).attr("url"));
                    });


                    let inAmt = 0;
                    let outAmt = 0;
                    $.each(res.data, function (i,d) {
                        if (d.close) {
                            return true;
                        }
                        if (!emptyEntity(d.inAmt)){
                            inAmt += parseFloat(d.inAmt);
                        }
                        if (!emptyEntity(d.outAmt)){
                            outAmt += parseFloat(d.outAmt);
                        }
                    });

                    setTotalRow("outAmt",getPrice(outAmt));
                    setTotalRow("inAmt",getPrice(inAmt));
                    setTotalRow("0","合计");

                    setTableLineCss("table",res.data, "close", true, "font-gray-c2c2c2");
                }
            });
        }

        function exportExcel() {
            genParam(genSearch());
            confirm("是否导出?",function () {
                tipLoad("处理中，请稍后");
                post("${baseUrl}funds/moneyWater/export.action"+genParam(genSearch()),{},function (resp) {
                    closeAll();
                    if (resp.code == 1) {
                        downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                    }  else {
                        errTip(resp.msg);
                    }
                });
            });
        }

        function genSearch() {
            let json = getFormJson($("#searchForm"));
            json.close = $('[name="noClose"]').next().hasClass("layui-form-checked") ? false:'';
            return json;
        }

        function clearSearch() {
            $("#searchForm")[0].reset();
            $('[name="date1"]').val('');
            $('[name="date2"]').val('');
            $('[name="makeTime1"]').val('');
            $('[name="makeTime2"]').val('');
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="table-tool-div">
            <form class="layui-form layui-form-pane" action="" id="searchForm">
                <input type="hidden" name="date1" value="${date1}">
                <input type="hidden" name="date2" value="${date2}">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="checkbox" name="noClose" title="不显示已关闭" lay-skin="primary" value="true" checked lay-filter="noClose">
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">搜索信息</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search" name="search" placeholder="单号、单位、备注" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <a class="layui-btn" lay-submit="" lay-filter="search" id="btn-search"><i class="layui-icon layui-icon-search"></i></a>
                        <a class="layui-btn" id="selectSearch">筛选</a>
                    </div>

                    <div class="table-tool-div-screen" status="close" style="display: none">
                        <div class="layui-inline">
                            <label class="layui-form-label">单据编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="no" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">往来单位</label>
                            <div class="layui-input-inline">
                                <input type="text" name="unitName" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline" >
                            <label class="layui-form-label">经手人</label>
                            <div class="layui-input-inline">
                                <select name="userId"></select>
                            </div>
                        </div>

                        <div class="layui-inline" >
                            <label class="layui-form-label">结算账户</label>
                            <div class="layui-input-inline">
                                <select name="settAccountId"></select>
                            </div>
                        </div>

                        <div class="layui-inline" >
                            <label class="layui-form-label">收支类型</label>
                            <div class="layui-input-inline">
                                <select name="type">
                                    <option value="">全部</option>
                                    <option value="1">收入</option>
                                    <option value="2">支出</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline" >
                            <label class="layui-form-label">收支项目</label>
                            <div class="layui-input-inline">
                                <select name="typeProject">
                                    <option value="">全部</option>
                                    <option value="1">销售</option>
                                    <option value="2">销售退货</option>
                                    <option value="3">进货</option>
                                    <option value="4">进货退货</option>
                                    <option value="5">转出金额</option>
                                    <option value="6">转入金额</option>
                                    <option value="7">日常收入</option>
                                    <option value="8">日常支出</option>
                                    <option value="9">支付欠款</option>
                                    <option value="10">收回欠款</option>
                                    <option value="11">调拨其他费用</option>
                                    <option value="12">出库运费</option>
                                    <option value="13">入库到付运费</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">业务日期</label>
                            <div class="layui-input-inline">
                                <input type="text" id="date" placeholder="请选择范围" class="layui-input" autocomplete="off">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">门店</label>
                            <div class="layui-input-inline">
                                <select name="storeId"></select>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                            <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                        </div>
                    </div>
                    <script>
                        $("#selectSearch").click(function () {
                            if ($(".table-tool-div-screen").attr("status")=="close") {
                                $(".table-tool-div-screen").css("display","block");
                                $(".table-tool-div-screen").attr("status","open");
                                $("#search").parent().parent().css("display","none");
                                $("#search").val("");
                                $("#btn-search").css("display","none");
                                $(this).html("收起");
                            } else {
                                $(".table-tool-div-screen").css("display","none");
                                $(".table-tool-div-screen").attr("status","close");
                                $("#search").parent().parent().css("display","inline-block");
                                $("#btn-search").css("display","inline-block");
                                $(this).html("筛选");
                            }
                        });

                        $("#clearSearch").click(function () {
                            clearSearch();
                        });
                    </script>
                </div>
            </form>
        </div>
        <hr/>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>

    </div>
</div>
</body>
<script type="text/html" id="tableToolBar">
    <div class="layui-inline">
        <a class="layui-btn layui-btn-primary" lay-event="export">导出</a>
    </div>
</script>

<script type="text/html" id="noBar">
    <a href="#" class="linkNo" title="{{d.typeName}}详情" url="${baseUrl}{{d.url}}.action?id={{d.id}}">{{d.no}}</a>
</script>

<html:loading/>
</html>